package com.controller.user;

import com.config.ProjConfig;
import com.entity.WalletRecord;
import com.pay.Service.AliPayService;
import com.pay.prjo.AliPayAsyncVo;
import com.pay.prjo.AliPayParam;
import com.service.user.WalletService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**  */
@RestController
@CrossOrigin
public class WalletController {

  /** 书币支付回调地址 */
  public static final String COIN_BUY_NOTIFY_URL = ProjConfig.host + "/wallet/buyCoinPayNotify";

  @Autowired WalletService walletService;
  /** 正在进行的书币充值，保存<payId,userId> */
  HashMap<String, String> allCoinBuying = new HashMap<>(100);

  @Autowired AliPayService aliPayService;

  /**
   * 修改收款二维码
   *
   * @param userId 用户名
   * @param file 新的二维码
   * @return 新的二维码的url
   */
  @PostMapping("wallet/chPaymentCodeUrl")
  public String chPaymentCodeUrl(
      @RequestParam("userId") String userId, @RequestParam("image") MultipartFile file) {
    return walletService.chPaymentCodeUrl(userId, file);
  }

  /**
   * 提现
   *
   * @param userId 用户编号
   * @param count 提现数量
   * @return 1 或 0
   */
  @PostMapping("wallet/requestWithdrawal")
  String requestWithdrawal(
      @RequestParam("userId") String userId, @RequestParam("count") float count) {
    int ret = walletService.requestWithdrawal(userId, count);
    return String.valueOf(ret);
  }

  @PostMapping("wallet/getWalletRecord")
  public List<WalletRecord> getWalletRecord(
      @RequestParam("userId") String userId,
      @RequestParam("type") int type,
      Integer pageSize,
      Integer pageIndex) {
    return walletService.getWalletRecord(userId, type, pageSize, pageIndex);
  }

  @PostMapping("wallet/getBuyCoinQrCode")
  public String getBuyCoinQrCode(
      @RequestParam("userId") String userId,
      @RequestParam("count") float count,
      @RequestParam("returnURL") String returnURL) {
    String payId = UUID.randomUUID().toString().replace("-", "");
    AliPayParam param = new AliPayParam();
    param.setOutTradeNo(payId);
    param.setSubject("书湘二手书交易平台书币");
    param.setTotalAmount(count);
    param.setTimeOut("10m");
    param.setNotifyUrl(COIN_BUY_NOTIFY_URL);
    param.setReturnUrl(returnURL);
    String qrCode = aliPayService.pay(param, null);
    if (qrCode != null) {
      allCoinBuying.put(payId, userId);
    }
    return qrCode;
  }

  @PostMapping("wallet/buyCoinPayNotify")
  public String buyCoinPayNotify(AliPayAsyncVo result) {
    return aliPayService.checkV1(
        result,
        () -> {
          System.out.println("书币购买成功");
          walletService.buyCoinSuccess(
              allCoinBuying.get(result.getOut_trade_no()),
              Float.parseFloat(result.getReceipt_amount()));
        },
        () -> allCoinBuying.remove(result.getOut_trade_no()));
  }
}
